package com.guli.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;
import com.guli.gulimall.product.dao.ProductAttrValueDao;
import com.guli.gulimall.product.dao.SkuImagesDao;
import com.guli.gulimall.product.dao.SkuInfoDao;
import com.guli.gulimall.product.dao.SpuInfoDescDao;
import com.guli.gulimall.product.entity.SkuImagesEntity;
import com.guli.gulimall.product.entity.SkuInfoEntity;
import com.guli.gulimall.product.entity.SpuInfoDescEntity;
import com.guli.gulimall.product.service.SkuInfoService;
import com.guli.gulimall.product.vo.CartItemVo;
import com.guli.gulimall.product.vo.ItemSaleAttrVo;
import com.guli.gulimall.product.vo.SkuItemVo;
import com.guli.gulimall.product.vo.spuItemAttrGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author WZH
 * @title: SkuInfoServiceImpl
 * @projectName gulimall-java
 * @description:
 * @date 2021/12/21
 */
@Service("skuInfoServiceImpl")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesDao skuImagesDao;

    @Autowired
    private SkuInfoDao skuInfoDao;

    @Autowired
    private SpuInfoDescDao spuInfoDescDao;

    @Autowired
    private ProductAttrValueDao productAttrValueDao;

    @Autowired
    private ThreadPoolExecutor threadPool;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SkuItemVo getItemVoBySkuId(Integer skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();
        //sku基本信息
        CompletableFuture<Long> f1 = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfo = baseMapper.selectById(skuId);
            skuItemVo.setSkuInfo(skuInfo);
            return skuInfo.getSpuId();
        }, threadPool);
        //是否有库存
//        Boolean hasStock=true;
        //sku的图片信息
        CompletableFuture<Void> f2 = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> images = skuImagesDao.selectList(new QueryWrapper<SkuImagesEntity>()
                    .eq("sku_id", skuId).orderByDesc("default_img"));
            skuItemVo.setImages(images);
        }, threadPool);
        //sku的销售属性
        /*
           属性id 属性名
           属性值 skuids
         */
        CompletableFuture<Void> f3 = CompletableFuture.runAsync(() -> {
            List<ItemSaleAttrVo> saleAttrs = skuInfoDao.getSaleAttr(skuId);
            skuItemVo.setSaleAttrs(saleAttrs);
        }, threadPool);

        //商品介绍
        CompletableFuture<Void> f4 = f1.thenAcceptAsync((spuId) -> {
            SpuInfoDescEntity desc = spuInfoDescDao.selectById(spuId);
            skuItemVo.setDesc(desc);
        }, threadPool);
        //spu规格属性
        /*
        分组名称
            属性名 属性值
         */
        CompletableFuture<Void> f5 = CompletableFuture.runAsync(() -> {
            List<spuItemAttrGroup> groupAttrs = productAttrValueDao.getSpuAttr(skuId);
            skuItemVo.setGroupAttrs(groupAttrs);
        }, threadPool);
        try {
            //阻塞
            CompletableFuture.allOf(f1,f2,f3,f4,f5).get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return skuItemVo;
    }

    @Override
    public CartItemVo getCart(String skuId) {
        SkuInfoEntity skuInfoEntity = baseMapper.selectById(skuId);
        CartItemVo cartItemVo = new CartItemVo();
        cartItemVo.setSkuId(skuInfoEntity.getSkuId());
        cartItemVo.setTitle(skuInfoEntity.getSkuTitle());
        cartItemVo.setImage(skuInfoEntity.getSkuDefaultImg());
        cartItemVo.setPrice(skuInfoEntity.getPrice());
        List<String> saleAttr = skuInfoDao.getSaleAttrAsStr(Integer.valueOf(skuId));
        cartItemVo.setSkuAttrValues(saleAttr);
        return cartItemVo;
    }
}
